package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.constant.MsgConstant;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.mq.NotifyMsgSendDto;
import com.cq.hd.common.enums.biz.NotifyMsgStateEnum;
import com.cq.hd.common.enums.biz.NotifyMsgTypeEnum;
import com.cq.hd.common.enums.biz.RoleTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.system.api.NotifyMsgApi;
import com.cq.hd.system.api.dto.NotifyMsgPageDto;
import com.cq.hd.system.api.dto.NotifyMsgUpdateReadDto;
import com.cq.hd.system.api.vo.NotifyMsgPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/notifyMsg")
@Api(tags = "消息通知相关接口")
public class NotifyMsgController {

    @Autowired
    private NotifyMsgApi notifyMsgApi;

//    @ApiOperation(value = "新增消息（测试用）")
//    @GetMapping("/save")
//    public Result<Boolean> save(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
//        NotifyMsgSendDto notifyMsgSendDto = new NotifyMsgSendDto();
//        notifyMsgSendDto.setUserId(appLoginUser.getUserId());
//        notifyMsgSendDto.setRoleType(RoleTypeEnum.BUSINESS.getValue());
//        notifyMsgSendDto.setMsgType(NotifyMsgTypeEnum.BUSINESS_MERCHANT_ENTER_APPLY.getValue());
//        notifyMsgSendDto.setContent(String.format(MsgConstant.BUSINESS_MERCHANT_ENTER_APPLY, "测试测试测试"));
//        notifyMsgSendDto.setMsgState(NotifyMsgStateEnum.NO_READ.getValue());
//        return notifyMsgApi.saveNotifyMsg(notifyMsgSendDto);
//    }

    @ApiOperation(value = "获取通知消息数据")
    @ApiResponse(response = NotifyMsgPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/pageNotifyMsg")
    public Result<Page<NotifyMsgPageVo>> pageNotifyMsg(NotifyMsgPageDto notifyMsgPageDto, @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        notifyMsgPageDto.setUserId(appLoginUser.getUserId());
        notifyMsgPageDto.setRoleType(RoleTypeEnum.BUSINESS.getValue());
        return notifyMsgApi.pageNotifyMsg(notifyMsgPageDto);
    }

    @ApiOperation(value = "更新消息已读状态")
    @PostMapping("/updateRead")
    public Result<Boolean> updateRead(@RequestBody NotifyMsgUpdateReadDto notifyMsgUpdateReadDto, @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        notifyMsgUpdateReadDto.setUserId(appLoginUser.getUserId());
        notifyMsgUpdateReadDto.setRoleType(RoleTypeEnum.BUSINESS.getValue());
        return notifyMsgApi.updateRead(notifyMsgUpdateReadDto);
    }

    @ApiOperation(value = "清除未读")
    @PostMapping("/cleanNoRead")
    public Result<Boolean> cleanNoRead(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        NotifyMsgUpdateReadDto notifyMsgUpdateReadDto = new NotifyMsgUpdateReadDto();
        notifyMsgUpdateReadDto.setUserId(appLoginUser.getUserId());
        notifyMsgUpdateReadDto.setRoleType(RoleTypeEnum.BUSINESS.getValue());
        return notifyMsgApi.cleanNoRead(notifyMsgUpdateReadDto);
    }

    @ApiOperation(value = "获取未读消息个数")
    @GetMapping("/getNoReadCount")
    public Result<Integer> getNoReadCount(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        NotifyMsgPageDto notifyMsgPageDto = new NotifyMsgPageDto();
        notifyMsgPageDto.setUserId(appLoginUser.getUserId());
        notifyMsgPageDto.setRoleType(RoleTypeEnum.BUSINESS.getValue());
        return notifyMsgApi.getNoReadCount(notifyMsgPageDto);
    }

}
